package Read;



import Definitions.GraphClass;
import Export.ExportClass;
import Global.ConstantVariable;
import Global.DirectoryStructureAndSettingsClass;
import Global.GlobalClass;
import Integrity.IntegrityCheckerClass;
import Integrity.IntegrityUtilityClass;
import Utility.FileUtilityClass;

public class ReadTest {

	public static String Test(int DataSet, GraphClass g, GlobalClass global)
	{		
		System.out.println("Start of Reading ");
		
		String inputDirectoryPath;
		String outputDirectoryPathForDsvFile="";
		String outputDirectoryPathForArffFile = null;
		
		String content=null;
		String cites=null;
		ReaderClass r = new ReaderClass();
		String splitchar=null;
		
		String datasetName="";
		String datasetFolderName="";
		
		inputDirectoryPath = ConstantVariable.DefaultDirectoryNamesConstants.ROOT_STATIC_DIRECTORY
							+ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderStaticRootDirectory.INPUTS_DIRECTORY;
		
		outputDirectoryPathForDsvFile = ConstantVariable.DefaultDirectoryNamesConstants.ROOT_DIRECTORY
										+ ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderRootDirectory.DATASETS_MAIN_DIRECTORY;
		
		String staticRootPath = ConstantVariable.DefaultDirectoryNamesConstants.ROOT_STATIC_DIRECTORY
		+ ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderRootDirectory.DATASETS_MAIN_DIRECTORY;
		
		/* TODO KADRIYEB change int to constant*/
		
		
		datasetName = ConstantVariable.DataSet_Constants.DATASET_NAMES[GlobalClass.RunningValues.executionFor];
		
		if(GlobalClass.RunningValues.executionFor!=ConstantVariable.DataSet_Constants.SYNTETIC_DATASET)
		{
			DirectoryStructureAndSettingsClass.setRootDirectoryNameForTheCurrentDataset(datasetName+"/");
			DirectoryStructureAndSettingsClass.setStaticRootDirectoryNameForTheCurrentDataset(datasetName+"/");
		}
		
		if(DataSet==ConstantVariable.DataSet_Constants.CITESEER)
		{					
			content = "citeseer.content";
			cites = "citeseer.cites";
			splitchar="	";			
			 
		}
		else if(DataSet==ConstantVariable.DataSet_Constants.CORA)
		{
			datasetName = "Cora";
			content = "cora.content";
			cites   = "cora.cites";
			splitchar="	";				
		}

		else if(DataSet==ConstantVariable.DataSet_Constants.WEBKB)
		{
			datasetName = "WebKB";
			content = "WebKB.content";
			cites   = "WebKB.cites";
			splitchar="	";				
		}
		else if(DataSet==ConstantVariable.DataSet_Constants.CUSTOM_DATASET)
		{
			datasetName = "Custom";
			System.out.println("Custom");
			content = "custom.content";
			cites   = "custom.cites";
			splitchar="	";				
		}
		else if(DataSet==ConstantVariable.DataSet_Constants.SYNTETIC_DATASET)
		{
			datasetFolderName = DirectoryStructureAndSettingsClass.getRootDirectoryNameForTheCurrentDataset();
			System.out.println("datasetFolderName:"+datasetFolderName);
			datasetName = DirectoryStructureAndSettingsClass.getcontentFileName(ConstantVariable.Common_ConstantVariables.DEFAULT_CONTENT_TYPE);
			System.out.println("datasetName:"+datasetName);
			System.out.println("SYN_DATASET");			
			content = DirectoryStructureAndSettingsClass.getcontentFileName(0)+ConstantVariable.DefaultDirectoryNamesConstants.FileExtensions.GEN_SYN_CONTENT_FILE_FORMAT_EXTENSION;
			cites   = DirectoryStructureAndSettingsClass.getLinksFileName(0)+ConstantVariable.DefaultDirectoryNamesConstants.FileExtensions.GEN_SYN_LINK_FILE_FORMAT_EXTENSION;
			splitchar="	";
			inputDirectoryPath=ConstantVariable.DefaultDirectoryNamesConstants.ROOT_STATIC_DIRECTORY
							  +ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderStaticRootDirectory.DATASETS_MAIN_DIRECTORY
							  +ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetsDirectory.SYNTHETIC_DATA_ROOT_FOLDER_NAME
							  +datasetFolderName
							  +ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderStaticDatasetDirectory.RAW_DATA_FOLDER_NAME;
			outputDirectoryPathForDsvFile += ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetsDirectory.SYNTHETIC_DATA_ROOT_FOLDER_NAME;
			outputDirectoryPathForDsvFile += datasetFolderName;		
			
			staticRootPath += ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetsDirectory.SYNTHETIC_DATA_ROOT_FOLDER_NAME;
			//outputDirectoryPathForDsvFile += datasetFolderName;
		} else if(DataSet==ConstantVariable.DataSet_Constants.WASHINGTON){
			datasetName = "WebKB_washington";
			System.out.println("WebKB_washington");
			content = "washington.content";
			cites   = "washington.cites";
			splitchar="	";			
		}			

		if(content!=null && cites!=null && splitchar!=null)
		{
			if(DataSet!=ConstantVariable.DataSet_Constants.SYNTETIC_DATASET)
			{
				outputDirectoryPathForDsvFile += (datasetName+"/");
				staticRootPath += (datasetName+"/");	
				
				DirectoryStructureAndSettingsClass.createDirectory(outputDirectoryPathForDsvFile);
				DirectoryStructureAndSettingsClass.createDirectory(staticRootPath);
				DirectoryStructureAndSettingsClass.createSubDirectoriesForADataset(outputDirectoryPathForDsvFile);
				DirectoryStructureAndSettingsClass.createSubDirectoriesForAStaticDataset(staticRootPath);
			}				
			

			
			outputDirectoryPathForDsvFile += ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderDatasetDirectory.WEKA_RELATED_FOLDER_NAME;
			outputDirectoryPathForArffFile = outputDirectoryPathForDsvFile; 
			outputDirectoryPathForDsvFile += ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderWekaRelatedDirectory.DSV_FORMAT_DATA_FOLDER_NAME;
			outputDirectoryPathForArffFile += ConstantVariable.DefaultDirectoryNamesConstants.DirectoriesUnderWekaRelatedDirectory.ARFF_FORMAT_DATA_FOLDER_NAME;
			
			r.Read(inputDirectoryPath + content, inputDirectoryPath + cites, splitchar, g, global);
			
			
			IntegrityCheckerClass integrityCheckerForTheNewDataset = new IntegrityCheckerClass(); 
			
			integrityCheckerForTheNewDataset.getIntegrityControlData().setCrcOfTheContentFile(IntegrityUtilityClass.calculateCRCOfTheFile(inputDirectoryPath + content));
			integrityCheckerForTheNewDataset.getIntegrityControlData().setCrcOfTheLinkFile(IntegrityUtilityClass.calculateCRCOfTheFile(inputDirectoryPath + cites));
			
			
			String fileNameForThePreviousIntegrityCheckFile = IntegrityCheckerClass.getFileNameForIntgerityCheckObject();   	
			
			
			IntegrityCheckerClass prevIntegrityCheckerForTheDataset = (IntegrityCheckerClass)FileUtilityClass.readObjectFromFile(fileNameForThePreviousIntegrityCheckFile);
			
			if(prevIntegrityCheckerForTheDataset!=null)
			{
				if(prevIntegrityCheckerForTheDataset.equals(integrityCheckerForTheNewDataset))
					GlobalClass.RunningValues.integrityCheckResult = true;
				else
					GlobalClass.RunningValues.integrityCheckResult = false;
			}
			
			FileUtilityClass.writeObjectToFile(integrityCheckerForTheNewDataset, IntegrityCheckerClass.getFileNameForIntgerityCheckObject());
		}

		System.out.println("End of Reading ");
		
		//System.out.println("outputDirectoryPathForDsvFile:"+outputDirectoryPathForDsvFile);
		//System.out.println("outputDirectoryPathForArffFile:"+outputDirectoryPathForArffFile);
		//System.exit(0);
		
		//System.out.println("Now exporting to DSV Format");
		
		String fullDirectoryPathForDsvFormat = outputDirectoryPathForDsvFile+datasetName+ConstantVariable.DefaultDirectoryNamesConstants.FileExtensions.DSV_FILE_FORMAT_EXTENSION;
		DirectoryStructureAndSettingsClass.addToLog("fullDirectoryPathForDsvFormat:"+fullDirectoryPathForDsvFormat);
		ExportClass.convertContentFeaturesToDsvFormat(g, global,
				ConstantVariable.Common_ConstantVariables.DEFAULT_CONTENT_TYPE,
				fullDirectoryPathForDsvFormat,
				ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.DEFAULT_DELIMITER,
				ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.DEFAULT_SETTING_TO_INCLUDE_HEADER,
				ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.DEFAULT_SETTING_TO_INCLUDE_NODE_IDS,
				ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.DEFAULT_SETTING_TO_INCLUDE_NODE_NAMES,
				ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.DEFAULT_SETTING_TO_INCLUDE_CLASS_LABELS,
				ConstantVariable.ExportNamesConstants.convertContentFeaturesToDsvFormatConstants.DEFAULT_SETTING_TO_USE_CLASS_ORDERS
				);
		
		String fullDirectoryPathForArffFormat = outputDirectoryPathForArffFile+datasetName+datasetName+ConstantVariable.DefaultDirectoryNamesConstants.FileExtensions.ARFF_FILE_FORMAT_EXTENSION;
		
		ExportClass.convertCSVFileToARFFFormat(fullDirectoryPathForDsvFormat, fullDirectoryPathForArffFormat);
		
		System.out.println("End of Reading.... "); 
		return "";//fullDirectoryPathForArffFormat;
	}
}
